<?php

namespace app\controller;

use app\BaseController;
use app\model\Recyclables as RecyclablesModel;
use app\Request;

class Recyclables extends BaseController
{
    /*
     * 发布回收物
     * */
    public function publishRecycle(Request $request)
    {
        //中间件保存的用户id和手机号
        $userId = $request->userId;

        //发布的回收物的信息
        $recyclableInfo = $this->request->param();
        //生成订单号
        //$orderNumber = date('YmdHis') . (mt_rand(1000, 9999) + intval($tel));
        $recyclablesId =$this->request->param('recyclablesId',0);
        if (empty($recyclableInfo["title"])||empty($recyclableInfo["upImage"])||empty($recyclableInfo["address"])||empty($recyclableInfo["price"])||empty($recyclableInfo["region"])) {
            var_dump(empty($recyclableInfo["title"]));
            return json(['code'=>555,'message'=>'参数错误','data',$recyclableInfo]);
        }
        //对省区数组转字符串，使用,分割
        $region = implode(',', $recyclableInfo["region"]);
        //要写入的数据
        $data=[
            "user_id" => $userId,
            "title" => $recyclableInfo["title"],
            "image" => $recyclableInfo["upImage"],
            "region"=>$region,
            "address" => $recyclableInfo["address"],
            "price" => $recyclableInfo["price"]
        ];
        $result = new RecyclablesModel;
        //如果传来的回收物id是0，则新增，否则是更新
        if ($recyclablesId == 0){
            $result ->save($data);
        }else{
            $result->save($data, ["id" => $recyclableInfo["recyclablesId"]]);
        }
        if (!$result) {//回收物信息写入数据库失败
            return json(['code' => 557, 'msg' => '发布失败']);
        }
        //写入成功，返回订单号
        return json(['code' => 200, 'msg' => '发布成功', 'id' => $result->id]);
    }

    /*
     * 获取回收物信息
     * */
    public function getRecycle()
    {
        $pageSize = $this->request->param('pageSize', 15); // 默认每页15条
        $currentPage = $this->request->param('currentPage', 1);// 默认第1页

        //回收物模型关联用户表模型，查询用户信息和对应回收物信息，每次加载4条
        $recyclables = RecyclablesModel::with(['user' => function ($query) {
            $query->field('id,nick,avatar');
        }])->where(['recyclables_statu'=>1])
            ->order('id', 'desc')
            ->paginate([//分页查询
                'page' => $currentPage,
                'list_rows' => $pageSize,
            ]);
        if ($recyclables->total()==0) {
            return json(['code' => 560, 'msg' => '这里啥也没有']);
        }
        return json(['code'=>200,'recyclables'=>$recyclables]);
    }

    /*
     * 搜索回收物
     * */
    public function search()
    {
        //获取搜索词
        $searchKeyCode = $this->request->param('searchKeyCode');
        $pageSize = $this->request->param('pageSize', 15); // 默认每页15条
        $currentPage = $this->request->param('currentPage', 1);// 默认第1页


        //判断有没有传入搜索词
        if (empty($searchKeyCode)) {
            return json(['code' => 555, 'msg' => '参数错误']);
        }
        //回收物模型关联用户表模型，查询回收物信息和对应的用户信息，模糊查询，分页处理
        $searchResult = RecyclablesModel::with(['user' => function ($query) {
            $query->field('id,nick,avatar');
        }])->where('title', 'like', '%' . $searchKeyCode . '%')
            ->paginate([
                'page' => $currentPage,
                'list_rows' => $pageSize,
            ]);

        //没有搜索到相关的回收物
        if ($searchResult->total()==0) {
            return json(['code' => 553, 'msg' => '没搜到对应回收物哦']);
        }
        //返回查询到的数据
        return json(['code' => 200, 'searchResult' => $searchResult]);
    }

    /*
     * 获取回收物详情
     * */
    public function getRecyclablesInfo()
    {
        //获取回收物的id
        $id = $this->request->param('id');
        //判断有没有传入id
        if (empty($id)) {
            return json(['code' => 555, 'msg' => '参数错误']);
        }
        //回收物模型关联用户表模型，查询回收物信息和对应的用户信息
        $recyclablesInfo = RecyclablesModel::with(['user' => function ($query) {
            $query->field('id,nick,avatar');
        }])->find($id);
        //返回查询到的信息
        return json(['code' => 200, 'recyclablesInfo' => $recyclablesInfo]);

    }
}